package hku.cs.eclunuo.run;

import hku.cs.eclunuo.LunuoConstant;
import hku.cs.eclunuo.LunuoPlugin;
import hku.cs.eclunuo.util.LunuoLibHelper;
import hku.cs.lunuo.constant.DirAndFileConstant;
import hku.cs.lunuo.instrument.Instrument;
import hku.cs.lunuo.instrument.TestCaseInstrument;
import hku.cs.lunuo.profile.PredsProfile;
import hku.cs.lunuo.tools.DirTools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import soot.Scene;
import soot.options.Options;

public class MainRunner {

	private IProject project;
	
	public MainRunner(IProject project) {
		super();
		this.project = project;
	}
	
	

	public void run(List<String> targetLst, List<String> testLst,
			List<String> copyLst, List<String> removeLst) {
		/*if (targetLst.size() != 0) {
			project = targetLst.get(0).getProject();
		} else if (testLst.size() != 0) {
			project = testLst.get(0).getProject();
		} else if (copyLst.size() != 0) {
			project = copyLst.get(0).getProject();
		} else if (removeLst.size() != 0) {
			project = removeLst.get(0).getProject();
		} else
			return;*/

		IJavaProject javaProject = JavaCore.create(project);

		/*
		 * List<String> filters; IResource resource = (IResource)
		 * project.getAdapter(IResource.class); try { String value = resource
		 * .getPersistentProperty(LunuoPlugin.FILTER_PROPERTY); if (value ==
		 * null) { filters = Arrays.asList(new String[0]); } else filters =
		 * Arrays.asList(value.split(";")); } catch (CoreException e) {
		 * LunuoLog.logError(e); filters = Arrays.asList(new String[0]); }
		 * 
		 * try { List<String> sourceDirs = new ArrayList<String>();
		 * IClasspathEntry xx[] = javaProject.getRawClasspath();
		 * 
		 * for (int i = 0; i < xx.length; i++) if (xx[i].getEntryKind() ==
		 * IClasspathEntry.CPE_SOURCE) {
		 * sourceDirs.add(xx[i].getPath().toString()); }
		 * 
		 * List<String> runClasses = new ArrayList<String>();
		 * 
		 * for (IResource je : instruLst) { String stre =
		 * je.getFullPath().toString(); for (String osd : sourceDirs) { if
		 * (stre.startsWith(osd)) { stre = stre.substring(osd.length() + 1,
		 * stre.length()); break; } } stre = stre.replace('/', '.'); stre =
		 * stre.substring(0, stre.lastIndexOf('.'));
		 * 
		 * if (stre.lastIndexOf('.') != -1) { if
		 * (!filters.contains(stre.substring(0, stre .lastIndexOf('.')))) {
		 * runClasses.add(stre); } } else { if
		 * (!filters.contains("(default package)")) { runClasses.add(stre); } }
		 * }
		 */
		// if (runClasses.size() > 0) {
		// String classpath = LunuoLibHelper
		// .urlsToString(getSootClassPath());
		// String output = ".";
		// output = LunuoPlugin.METADIR.append(
		// javaProject.getOutputLocation()).toOSString();
		// int i = runClasses.size() / 4;
		// for (int j = 1; j <= 4; j++) {
		// soot.G.reset();
		//
		// Scene.v().setSootClassPath(classpath);
		//
		// Options.v().set_output_dir(output);
		// List<String> subLst;
		// if (j == 4)
		// subLst = runClasses.subList(i * (j - 1), runClasses
		// .size() - 1);
		// else
		// subLst = runClasses.subList(i * (j - 1), i * j - 1);
		// String[] rrss = runClasses.toArray(new String[0]);
		// Instrument.main(rrss);
		// }
		//
		// i = runClasses.size() / 4;
		// for (int j = 1; j <= 4; j++) {
		// soot.G.reset();
		// Scene.v().setSootClassPath(classpath);
		// PredsProfile pp = new PredsProfile();
		// output = LunuoPlugin.METADIR.append(
		// javaProject.getProject().getName() + File.separator
		// + DirAndFileConstant.PRED).toOSString();
		// DirTools.prepare(output);
		// List<String> subLst;
		// if (j == 4)
		// subLst = runClasses.subList(i * (j - 1), runClasses
		// .size() - 1);
		// else
		// subLst = runClasses.subList(i * (j - 1), i * j - 1);
		// String[] rrss = runClasses.toArray(new String[0]);
		// pp.profile(rrss, new File(output));
		// }
		
		IPath metaPjtPath = LunuoPlugin.METADIR.append(
				javaProject.getProject().getName() + File.separator);
		String classpath = LunuoLibHelper.urlsToString(LunuoLibHelper.getSootClassPath(javaProject));
		String output = ".";
		
		try {
			output = LunuoPlugin.METADIR.append(javaProject.getOutputLocation()).toOSString();
		} catch (JavaModelException e1) {
			e1.printStackTrace();
		}
	
		
		
		if (testLst.size() > 0) {
			soot.G.reset();
			Scene.v().setSootClassPath(classpath);
			String[] rrss = testLst.toArray(new String[0]);
			try {
				new TestCaseInstrument().modifyTests(rrss,
						metaPjtPath.toString()+LunuoConstant.INSTRDEFAULTOUTDIR, output);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
//		if (targetLst.size() > 0) {
//			soot.G.reset();
//			Scene.v().setSootClassPath(classpath);
//			Options.v().set_output_dir(output);
//			String[] rrss = targetLst.toArray(new String[0]);
//			Instrument.main(rrss);
//
//			soot.G.reset();
//			Scene.v().setSootClassPath(classpath);
//			PredsProfile pp = new PredsProfile();
//			output = metaPjtPath.append(
//							DirAndFileConstant.PRED).toOSString();
//			DirTools.prepare(output);
//			pp.profile(rrss, new File(output));
//		}
		

		
		
		
		
		
		for (String file : copyLst) {
			File f = LunuoPlugin.METADIR.append(file).toFile();
			if(!f.exists()) {
				File p = project.getWorkspace().getRoot().getLocation().append(file).toFile();
				if(p.exists()) {
					if(p.isDirectory())
						f.mkdirs();
					if(p.isFile())
						try {
							DirTools.copy(p, f);
						} catch (Exception e) {
							e.printStackTrace();
						}
				}
			}
		}
		
		
		for (String file : removeLst) {
			DirTools.delete(LunuoPlugin.METADIR.append(file).toFile());
		}
	}

	

	
}
